package com.rose.datax.dataxadmin.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rose.datax.dataxadmin.action.request.DbConnectionRequest;
import com.rose.datax.dataxadmin.entity.*;
import com.rose.datax.dataxadmin.mapper.DbConnectionMapper;
import com.rose.datax.dataxadmin.utils.DbUtils;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
@Controller
public class DbAction extends BaseAction{

    @Resource
    private DbConnectionMapper connMapper;

    @GetMapping(value="/db/connections",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Response connections(){
        Page<DbConnection> page = getPage();
        IPage<DbConnection> pages = connMapper.selectPage(page, new QueryWrapper<DbConnection>().orderByAsc("id"));
        Response response = new Response();
        response.setTotal(pages.getTotal());
        response.setItems(pages.getRecords());
        return response;
    }

    @RequestMapping(value = "/db/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public JSONResult save(@RequestBody DbConnectionRequest request){
        DbConnection connection = request.convert(DbConnection.class);
        connMapper.insert(connection);
        return new JSONResult();
    }

    @RequestMapping(value = "/db/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public JSONResult get(@PathVariable long id){
        DbConnection conn = connMapper.selectById(id);
        return new JSONResult<DbConnection>(conn);
    }

    @RequestMapping(value = "/db/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public JSONResult update(@PathVariable long id, @RequestBody DbConnectionRequest request){
        DbConnection conn = request.convert(DbConnection.class);
        connMapper.updateById(conn);
        return new JSONResult<DbConnection>(conn);
    }

    @RequestMapping(value = "/db/test", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public JSONResult test(@RequestBody DbConnectionRequest request){
        DbConnection conn = request.convert(DbConnection.class);
        try {
            @Cleanup
            Connection connection = DbUtils.getConnection(conn.getJdbcURL(), conn.getUsername(), conn.getPassword());
            if(null != connection){
                return new JSONResult();
            }
        }catch (SQLException e){
            log.error(e.getMessage());
        }
        return new JSONResult("500", "数据库连接失败");
    }

}
